import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: elin11
 * Date: Jul 28, 2008
 * Time: 9:38:55 AM
 * To change this template use File | Settings | File Templates.
 */
public class AccessionParse{
//makes a file that contains a list of accession numbers and that we're looking for
	
	private BufferedWriter outputfilewriter;
	private ArrayList<String> ascendList;
    public AccessionParse(String outputfile) throws IOException {
    	outputfilewriter = new BufferedWriter(new FileWriter(outputfile));    	
    }

    public void grabSequences(String parsefile, boolean flagUniqueAccessions)throws IOException{
		// grabs the sequence and header of an interested accession number if the number is contained within
    	// the header of the parsefile/database proffered
    	BufferedReader parsefilereader= new BufferedReader(new FileReader(parsefile));
		String readline = null;
	    boolean found = false;
	    Iterator<String> accessIter;
	    String tempaccess;
	    int counter = 0;
	    int counter2 = 0;
	    boolean done = false;
	    while (((readline=parsefilereader.readLine()) != null) && !done){
	    	//basically checks to see if there is a > and if there is, check to see if it contains
	    	//the Accession number from the accessiongeneratecolumn function arraylist.
	    	if(readline.charAt(0) == '>'){
	    		counter++;
	    		found = false;
	    		accessIter = ascendList.iterator();
	    		while(accessIter.hasNext() && found == false){
	    			tempaccess = accessIter.next();
	    			if(readline.contains(tempaccess)){
	    				if(flagUniqueAccessions){
	    					// set to true if accessionnumbers will not correspond to multiple
	    					//entries within the database
	    					ascendList.remove(tempaccess);
	    					if(ascendList.isEmpty())
	    						done = true;
	    				}
	    				found = true;
	    				System.out.println(counter2++ + " found");
	    			}
	    		}
	    		if(counter % 10000 == 0)
		    		System.out.println("peptides searched: " + counter);
	    	}
	    	if(found){
    			outputfilewriter.write(readline + "\r\n");
	    	}
	    }
	    System.out.println("closing parsefile");
	    parsefilereader.close();
	}
    public void grabInternalSequences(String ascendlist, int keycolumn1, String delimiter)throws IOException{
    	// assumes that the file containing the accessionnumbers also contains pertinent
    	// sequence information/ repeate of grabsequences but does not query database
    	BufferedReader ascreader = new BufferedReader(new FileReader(ascendlist));
    	String readline = null;
    	String[] Tokenized;
    	boolean found = false;
    	int i = 0;
    	String temp;
 	    while ((readline= ascreader.readLine()) != null) {
 	    	Tokenized = readline.split(delimiter);
 	    	outputfilewriter.write(">"+Tokenized[keycolumn1-1]);
 	    	outputfilewriter.newLine();
 	    	i = Tokenized.length-1;
 	    	found = false;
 	    	while(i>0 && !found){
 	    		temp = Tokenized[i];
 	    		if(temp.length()>5 && temp.replaceAll("[A-Z]", "").isEmpty()){
 	    			outputfilewriter.write(Tokenized[i]);
 	    			found = true;
 	    		}
 	    		i--;
 	    	}
 	    	outputfilewriter.newLine();
 	    }
    	
    }
    public void displaySample(String ascendlist, String delimiter, int number) throws IOException{
    	// made to make defining the pertinent column easier
    	// displays a sampleline with each segment numbered
    	BufferedReader ascreader = new BufferedReader(new FileReader(ascendlist));
    	String readline = null;
    	String[] Tokenized;
    	for(int j = 0;j<number;j++){
 	    if((readline= ascreader.readLine()) != null) {
 	    	Tokenized = readline.split(delimiter);
 	    	for(int i = 0;i<Tokenized.length;i++){
 	    		System.out.println( (i+1) + ". " + Tokenized[i]);
 	    	}
 	    }
    	}
    }
    public void accessionGenerateColumn(String ascendlist,int keyColumn, String delimiter) throws IOException{
    	// makes an arraylist of the accession numbers from accession number file based on the key column and delimiter
    	BufferedReader ascreader = new BufferedReader(new FileReader(ascendlist));
    	String readline = null;
    	String[] Tokenized;
 	   	ascendList = new ArrayList<String>();
 	    while ((readline= ascreader.readLine()) != null) {
 	    	Tokenized = readline.split(delimiter);
 	    	ascendList.add(Tokenized[keyColumn-1]);
 	    	System.out.println(Tokenized[keyColumn-1]);
 	    }
    }
	protected void finalize() throws Throwable{
		outputfilewriter.close();	
	}
//"D:\Z drive backup\Databases\NCBInr_20080418.fasta"
/*    public static void main(String[] args)throws Throwable{
    	//initial test driver for program.
    	long average = 0;
    	long time;
    	String accessionfilename = args[0];
    	String outputfile = args[1];
    	String parsefilename;
    		time = System.currentTimeMillis();
        	accessionfilename = "AccessionList3.txt";
			
			AccessionParse parseObject= new AccessionParse(outputfile); 
			parseObject.accessionGenerateColumn(accessionfilename, 1, "\\s");
			
			
		    for(int j = 2;j<args.length;j++){                                     
		    	parsefilename = args[j];
		    	parseObject.grabSequences(parsefilename,true);
		    }
		    parseObject.finalize();    
		    average = System.currentTimeMillis()-time;
		    System.out.println(average + "milliseconds");
    }*/
}
